package guice.boot.archive.container.registry;

import guice.boot.archive.spi.registry.ServiceProvider;
import guice.boot.archive.spi.registry.ServiceProviderType;
import guice.boot.archive.spi.service.PriorityOrdered;


public abstract class AbstractServiceProvider implements ServiceProvider {

    protected ServiceProviderType serviceProviderType;

    public AbstractServiceProvider(ServiceProviderType serviceProviderType) {
        this.serviceProviderType = serviceProviderType;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }

        if (obj == null || obj.getClass() != this.getClass()) {
            return false;
        }

        if (getPriority() != ((PriorityOrdered) obj).getPriority()) {
            return false;
        }

        return getServiceProviderType() == ((ServiceProvider) obj).getServiceProviderType();
    }

    @Override
    public ServiceProviderType getServiceProviderType() {
        return serviceProviderType;
    }

    @Override
    public String getServiceProviderDesc() {
        return getServiceProviderType().getDesc();
    }

    @Override
    public String toString() {
        return String.format("ServiceProvider{provider=\'%s\', order=%d}",
                getServiceProviderDesc(), getPriority());
    }

    @Override
    public int hashCode() {
        int result = getServiceProviderType().hashCode();
        result = 31 * result + getPriority();
        return result;
    }
}